global class OutboundEmails {
    
    public OutboundEmails(Announcement_Controller controller) {
    }
    
        static WebService void SendEmailWithTemplate(ID announcement_id) {
        
        List<A_Task__c> tsk_c= new List<A_Task__c>( [ SELECT Id FROM A_Task__c WHERE Announcement__c = :Announcement_id]);
        Set<Id> ids_tsks = new Set<Id>();
        for(A_Task__c t_c : tsk_c){    
            ids_tsks.add(t_c.Id);
        }
        EmailTemplate et = [SELECT id, name FROM EmailTemplate WHERE Name = 'New Announcement' LIMIT 1];          
      
        List<A_Task_User__c> tsk_u = new List<A_Task_User__c>([SELECT User__c FROM A_Task_User__c WHERE A_Task_User__c.Announcement_Task__c IN :ids_tsks]);
        Set<Id> ids_user = new Set<Id>();
        for(A_Task_User__c t_u : tsk_u){    
            ids_user.add(t_u.User__c);
        }
        
        List<User> usrs = new List<User>( [SELECT ContactId FROM User WHERE Id IN :ids_user]);
        Set<Id> ids_contact = new Set<Id>();
        for(User usr : usrs){    
            ids_contact.add(usr.ContactId);
        }        
        
        List<Contact> contacts = new List<Contact>([SELECT Id,firstname FROM Contact WHERE Id IN :ids_contact]);
        for(Contact recipient : contacts){
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    
            mail.setTemplateId(et.id);
            mail.setWhatId(Announcement_id);       
            mail.setTargetObjectId(recipient.id); // Specify who the email should be sent to.
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        }
    }

}